<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:p="http://primefaces.org/ui"
	template="/WEB-INF/templates/default.xhtml"
 	xmlns:sec="http://www.springframework.org/security/facelets/tags">  
	<ui:define name="content">
		<!-- Formulário de Pesquisa -->
		
		<h:form id="frmPesquisa" styleClass="form-horizontal" prependId="false">
			<p:outputPanel id="panelPesquisa" rendered="#{!chamadoController.mostraCadastro}">
				<fieldset>
					<legend>#{messages.TITLE_ABERTURA_CHAMADO}</legend>
	
					<!-- Campos de Pesquisa -->
					<div class="control-group">
						<h:outputLabel for="txtResumoSearch" value="#{messages.LABEL_RESUMO}:" styleClass="control-label"/>
	                    <div class="controls">
							<p:inputText value="#{chamadoController.chamadoSearch.resumo}" id="txtResumoSearch" styleClass="input-xxlarge"/>
	                    </div>
					</div>

					<div class="control-group">
						<h:outputLabel for="txtSolicitanteSearch" value="#{messages.LABEL_SOLICITANTE}:" styleClass="control-label"/>
	                    <div class="controls">
							<p:inputText value="#{chamadoController.solicitanteSearch.nome}" id="txtSolicitanteSearch" styleClass="input-xxlarge"/>
	                    </div>
					</div>

					<div class="control-group">
						<h:outputLabel for="cmbUnidadeSearch" value="#{messages.LABEL_UNIDADE}:" styleClass="control-label"/>
						<div class="controls">
							<h:selectOneMenu id="cmbUnidadeSearch" value="#{chamadoController.chamadoSearch.unidade}" label="#{messages.LABEL_UNIDADE}"
								converter="entityConverter">
								<f:selectItem itemLabel="Selecione a #{messages.LABEL_UNIDADE}"/>
								<f:selectItems value="#{chamadoController.unidades}" var="unidade" itemLabel="#{unidade.nome}"/>
								<p:ajax event="change" update="cmbDepartamentoSearch" listener="#{chamadoController.changeDepartamentoSearch}"/>
							</h:selectOneMenu>
						</div>
					</div>
	
					<div class="control-group">
						<h:outputLabel for="cmbDepartamentoSearch" value="#{messages.LABEL_DEPARTAMENTO}:" styleClass="control-label"/>
						<div class="controls">
							<h:selectOneMenu id="cmbDepartamentoSearch" value="#{chamadoController.chamadoSearch.departamento}" label="#{messages.LABEL_DEPARTAMENTO}"
								converter="entityConverter">
								<f:selectItem itemLabel="Selecione o #{messages.LABEL_DEPARTAMENTO}"/>
								<f:selectItems value="#{chamadoController.departamentos}" var="departamento" itemLabel="#{departamento.nome}"/>
							</h:selectOneMenu>
						</div>
					</div>
					<!--End Campos de Pesquisa -->
	
					<!-- Primeiro Grupo de botões -->
					<div class="form-actions">
						<p:commandButton id="btnPesquisar" value="#{messages.LABEL_PESQUISAR}" action="#{chamadoController.pesquisar()}"
							process="frmPesquisa" update="frmPesquisa" styleClass="btn btn-primary"/>
							
						<p:commandButton id="btnLimpar" value="#{messages.LABEL_LIMPAR}" action="#{chamadoController.limpar()}"
							process="@this" update="frmPesquisa" styleClass="btn btn-primary"/>
	
						<p:commandButton id="btnNovo" value="#{messages.BTN_ABRIR_CHAMADO}" action="#{chamadoController.abrir()}" process="@this"
	                    	update="frmPesquisa, :frmCadastro" styleClass="btn btn-primary"/>
	                </div>
	                <!--End Primeiro Grupo de botões -->
				</fieldset>
	
				<!-- Grid de dados -->
				<p:dataTable id="tblChamado" var="chamado" value="#{chamadoController.chamados}" rows="10" paginator="true" 
	           		style="#{empty chamadoController.chamados ? 'display:none;' : 'display:table; width: 100%;'}" styleClass="table table-bordered">

	             	<f:facet name="header">
	             		<h:outputText value="#{messages.TITLE_PESQUISAR}"/>
	     			</f:facet>

					<p:column headerText="#{messages.LABEL_RESUMO}" >  
	                    <h:outputText value="#{chamado.resumo}"/>
	                </p:column>
	                
	                <p:column headerText="#{messages.LABEL_DESCRICAO_PROBLEMA}" >  
	                    <h:outputText value="#{chamado.descricao}"/>
	                </p:column>

					<p:column headerText="#{messages.LABEL_SOLICITANTE}" >
	                    <h:outputText value="#{chamado.usuarioSolicitacao.nome}"/>
	                </p:column>

					<p:column headerText="#{messages.LABEL_UNIDADE}" >  
	                    <h:outputText value="#{chamado.unidade.nome}"/>
	                </p:column>

	     			<p:column headerText="#{messages.LABEL_DEPARTAMENTO}" >  
	                    <h:outputText value="#{chamado.departamento.nome}"/>
	                </p:column>

					<p:column headerText="#{messages.LABEL_STATUS}" >  
	                    <h:outputText value="#{chamado.status}"/>
	                </p:column>

					<!-- Segundo Grupo de botões -->
                	<p:column headerText="#{messages.LABEL_AVALIAR}" style="text-align: center !important;" rendered="#{!usuarioBean.isServidor()}">
	               		<p:commandLink id="btnAvaliar" title="#{messages.LABEL_AVALIAR}" action="#{chamadoController.avaliar(chamado)}"
	                		update=":frmPesquisa, :frmCadastro">
	                		<p:graphicImage value="#{resource['images/edit.png']}" />
	                	</p:commandLink>
	                </p:column>

					<p:column headerText="#{messages.LABEL_AUTORIZAR}" style="text-align: center !important;" rendered="#{usuarioBean.isGerente() or usuarioBean.isAdministrador()}">
	               		<p:commandLink id="btnAprovar" title="#{messages.LABEL_APROVAR}" onclick="confirmModalAprovar.show();"
	               			rendered="#{chamado.isAvaliado == 1}">
	               			<f:setPropertyActionListener value="#{chamado}" target="#{chamadoController.chamado}"/>
	                		<p:graphicImage value="#{resource['images/circle_green.png']}" />
	                	</p:commandLink>

	                	<p:commandLink id="btnAprovarException" title="#{messages.LABEL_APROVAR}" action="#{chamadoController.validarAprovacao(chamado)}"
	               			rendered="#{chamado.isAvaliado == 0}" oncomplete="Util.scrollTop();">
	                		<p:graphicImage value="#{resource['images/circle_green.png']}" />
	                	</p:commandLink>

						<p:commandLink id="btnReprovar" title="#{messages.LABEL_REPROVAR}" onclick="modalReprovar.show();"
	                		process="@this" update=":frmReprovar" rendered="#{chamado.isAvaliado == 1}" styleClass="margin_left">
	                		<f:setPropertyActionListener value="#{chamado}" target="#{chamadoController.chamado}"/>
	                		<p:graphicImage value="#{resource['images/circle_red.png']}" />
	                	</p:commandLink>

						<p:commandLink id="btnReprovarException" title="#{messages.LABEL_REPROVAR}" action="#{chamadoController.validarReprovacao(chamado)}"
	                		rendered="#{chamado.isAvaliado == 0}" styleClass="margin_left" oncomplete="Util.scrollTop();">
	                		<p:graphicImage value="#{resource['images/circle_red.png']}" />
	                	</p:commandLink>
	                </p:column>
	                <!-- End Segundo Grupo de botões -->
				</p:dataTable>
				<!-- End Grid de dados -->
			</p:outputPanel>
		</h:form>
		<!-- End Formulário de Pesquisa -->

		<!-- Formulário de Cadastro -->
		<h:form id="frmCadastro" prependId="false" styleClass="form-horizontal">
			<p:outputPanel id="panelCadastro" rendered="#{chamadoController.mostraCadastro}">
				<legend style="#{chamadoController.isAbrir ? '' : 'display:none;'}">#{messages.TITLE_ABRIR_CHAMADO}</legend>
				<legend style="#{chamadoController.isAvaliar ? '' : 'display:none;'}">#{messages.TITLE_AVALIAR_CHAMADO}</legend>

				<div class="control-group">
					<h:outputLabel for="txtSolicitante" value="#{messages.LABEL_SOLICITANTE}:" styleClass="control-label required"/>
                    <div class="controls">
						<p:inputText id="txtSolicitante" value="#{chamadoController.chamado.usuarioSolicitacao.nome}" disabled="true" maxlength="50"
							label="#{messages.LABEL_SOLICITANTE}" styleClass="input-xxlarge"/>
					</div>
				</div>

				<div class="control-group">
					<h:outputLabel for="cmbUnidade" value="#{messages.LABEL_UNIDADE}:" styleClass="control-label required"/>
					<div class="controls">
						<h:selectOneMenu id="cmbUnidade" value="#{chamadoController.chamado.unidade}" label="#{messages.LABEL_UNIDADE}"
							converter="entityConverter" disabled="#{chamadoController.isAvaliar}">
							<f:selectItem itemLabel="Selecione a #{messages.LABEL_UNIDADE}"/>
							<f:selectItems value="#{chamadoController.unidades}" var="unidade" itemLabel="#{unidade.nome}"/>
							<p:ajax event="change" update="cmbDepartamento" listener="#{chamadoController.changeDepartamento}"/>
						</h:selectOneMenu>
					</div>
				</div>

				<div class="control-group">
					<h:outputLabel for="cmbDepartamento" value="#{messages.LABEL_DEPARTAMENTO}:" styleClass="control-label required"/>
					<div class="controls">
						<h:selectOneMenu id="cmbDepartamento" value="#{chamadoController.chamado.departamento}" label="#{messages.LABEL_DEPARTAMENTO}"
							converter="entityConverter" disabled="#{chamadoController.isAvaliar}">
							<f:selectItem itemLabel="Selecione o #{messages.LABEL_DEPARTAMENTO}"/>
							<f:selectItems value="#{chamadoController.departamentos}" var="departamento" itemLabel="#{departamento.nome}"/>
						</h:selectOneMenu>
					</div>
				</div>

				<div class="control-group">
					<h:outputLabel for="txtResumo" value="#{messages.LABEL_RESUMO}:" styleClass="control-label required"/>
					<div class="controls">
						<p:inputText id="txtResumo" value="#{chamadoController.chamado.resumo}" maxlength="50"
							label="#{messages.LABEL_RESUMO}" styleClass="input-xxlarge" disabled="#{chamadoController.isAvaliar}"/>
					</div>
				</div>
				
				<div class="control-group">
					<h:outputLabel for="txtDescricaoProblema" value="#{messages.LABEL_DESCRICAO_PROBLEMA}:" styleClass="control-label required"/>
                    <div class="controls">
						<p:inputTextarea id="txtDescricaoProblema" value="#{chamadoController.chamado.descricao}"
							label="#{messages.LABEL_DESCRICAO_PROBLEMA}" disabled="#{chamadoController.isAvaliar}" style="width: 535px;"/>
					</div>
				</div>

				<div class="control-group" style="#{chamadoController.isAvaliar ? '' : 'display:none;'}">
					<h:outputLabel for="rdPrioridade" value="#{messages.LABEL_PRIORIDADE}:" styleClass="control-label required"/>
                    <div class="controls">
						<h:selectOneMenu id="rdPrioridade" value="#{chamadoController.chamado.prioridade}">  
				            <f:selectItem itemLabel="Baixa" itemValue="Baixa"/>  
				            <f:selectItem itemLabel="Média" itemValue="Média"/>  
				            <f:selectItem itemLabel="Alta" itemValue="Alta"/>  
				        </h:selectOneMenu>
					</div>
				</div>

				<div class="form-actions">
					<p:commandButton id="btnSalvar" value="#{messages.BTN_SALVAR}" action="#{chamadoController.salvar()}"
	                	update=":frmCadastro, :frmPesquisa" styleClass="btn" oncomplete="Util.scrollTop();" process="frmCadastro"/>
	                	
	                <p:commandButton value="#{messages.LABEL_CANCELAR}" action="#{chamadoController.cancelar()}" 
						update=":frmCadastro, :frmPesquisa" immediate="true" styleClass="btn"/>
				</div>
			</p:outputPanel>
		</h:form>
		
		<!-- Dialog de confirmação ações -->
       	<h:form prependId="false">
			<p:confirmDialog message="#{messages.TITLE_HEADER_APROVAR}" header="Confirmação" severity="alert" widgetVar="confirmModalAprovar">
				<p:commandButton value="#{messages.LABEL_SIM}" inline="true" process="@this" actionListener="#{chamadoController.aprovar()}"
					onclick="confirmModalAprovar.hide();" update=":frmPesquisa" oncomplete="Util.scrollTop();"/>

				<p:commandButton value="#{messages.LABEL_NAO}" type="button" onclick="confirmModalAprovar.hide();"/>
			</p:confirmDialog>
		</h:form>
		
		<p:dialog widgetVar="modalReprovar" header="#{messages.TITLE_HEADER_REPROVAR}" resizable="false" modal="true">
			<h:form id="frmReprovar">
				<p:messages autoUpdate="true"></p:messages>

				<div class="control-group">
					<h:outputLabel for="txtMotivoReprovacao" value="#{messages.LABEL_MOTIVO_REPROVACAO}:" styleClass="control-label required"/>
                    <div class="controls">
						<p:inputTextarea id="txtMotivoReprovacao" value="#{chamadoController.chamado.motivoReprovacao}"
							label="#{messages.LABEL_MOTIVO_REPROVACAO}" style="width: 250px;"/>
					</div>
				</div>

				<div class="form-actions">
					<p:commandButton id="btnSalvar" value="#{messages.BTN_SALVAR}" action="#{chamadoController.reprovar()}"
	                	update=":frmPesquisa" styleClass="btn" oncomplete="Util.scrollTop();" process="frmReprovar"/>
	                	
	                <p:commandButton value="#{messages.LABEL_FECHAR}" styleClass="btn" type="button" onclick="modalReprovar.hide();"/>
				</div>
			</h:form>
		</p:dialog>
       	<!-- End Dialog de confirmação de ações -->
	</ui:define>
</ui:composition>